package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.heaven7.android.dragflowlayout.ClickToDeleteItemListenerImpl;
import com.heaven7.android.dragflowlayout.DragAdapter;
import com.heaven7.android.dragflowlayout.DragFlowLayout;
import com.heaven7.android.dragflowlayout.IViewObserver;
import com.iflytek.cloud.thirdparty.V;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.DragItem;
import com.sctvcloud.dongqu.beans.FocusColumn;
import com.sctvcloud.dongqu.ui.fragment.HomeFragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.sctvcloud.dongqu.ui.fragment.HomeFragment.COLUMN_LIST;

/**
 * 栏目管理页面
 */
public class ColumnEditActivity extends BaseActivity {
    @BindView(R.id.to_edit)
    protected CustomFontTextView toEdit;
    @BindView(R.id.my_column)
    protected DragFlowLayout myColumn;
    @BindView(R.id.other_column)
    protected DragFlowLayout otherColumn;
    public static final int RESULT_CODE = 2;
    public static final String COLUMN = "column";
    public static final String SELECT_POS = "select_pos";
    private boolean isEdit = false;
    private boolean isCanMove = false;
    private ArrayList<FocusColumn> columnList;
    private ArrayList<FocusColumn> newColumnList = new ArrayList<>();
    private DragItem moveItem;
    private List<DragItem> myDrag = new ArrayList<>();
    private List<DragItem> otherDrag = new ArrayList<>();
    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_column_edit);
        ButterKnife.bind(this);
    }

    private void initView() {

        columnList = (ArrayList<FocusColumn>) getIntent().getSerializableExtra(HomeFragment.COLUMN_DATA);
//        //添加view观察者
//        myColumn.addViewObserver(new IViewObserver() {
//            @Override
//            public void onAddView(View child, int index) {
//                if (isCanMove) {
//                    myDrag.add(index, moveItem);
//                }
//            }
//
//            @Override
//            public void onRemoveView(View child, int index) {
//                if (isCanMove) {
//                    moveItem = myDrag.get(index);
//                    myDrag.remove(index);
//                }
//            }
//        });

        //DragAdapter 泛型参数就是为了每个Item绑定一个对应的数据。通常很可能是json转化过来的bean对象
        myColumn.setDragAdapter(new DragAdapter<DragItem>() {

            @Override  //获取你的item布局Id
            public int getItemLayoutId() {
                return R.layout.item_drag_flow;
            }

            //绑定对应item的数据
            @Override
            public void onBindData(View itemView, int dragState, final DragItem data) {
                itemView.setTag(data);
                ImageView close = (ImageView) itemView.findViewById(R.id.iv_close);
                if (isEdit && data.isCanDrag()) {
                    close.setVisibility(View.VISIBLE);
                } else {
                    close.setVisibility(View.INVISIBLE);
                }
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myDragRemove(data);
                    }
                });
                TextView tv = (TextView) itemView.findViewById(R.id.title);
                tv.setText(data.getTitle());
//                //iv_close是关闭按钮。只有再非拖拽空闲的情况下才显示
//                itemView.findViewById(R.id.iv_close).setVisibility(
//                        dragState != DragFlowLayout.DRAG_STATE_IDLE
//                                && data.isCanDrag() ? View.VISIBLE : View.INVISIBLE);
            }

            //根据指定的child获取对应的数据。
            @NonNull
            @Override
            public DragItem getData(View itemView) {
                return (DragItem) itemView.getTag();
            }
        });

        //DragAdapter 泛型参数就是为了每个Item绑定一个对应的数据。通常很可能是json转化过来的bean对象
        otherColumn.setDragAdapter(new DragAdapter<DragItem>() {

            @Override  //获取你的item布局Id
            public int getItemLayoutId() {
                return R.layout.item_drag_add_flow;
            }

            //绑定对应item的数据
            @Override
            public void onBindData(View itemView, int dragState, final DragItem data) {
                itemView.setTag(data);
                ImageView add = (ImageView) itemView.findViewById(R.id.iv_add);
                if (isEdit) {
                    add.setVisibility(View.VISIBLE);
                } else {
                    add.setVisibility(View.INVISIBLE);
                }
                add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        OtherAdd(data);
                    }
                });
                TextView tv = (TextView) itemView.findViewById(R.id.title);
                tv.setText(data.getTitle());
//                //iv_close是关闭按钮。只有再非拖拽空闲的情况下才显示
//                itemView.findViewById(R.id.iv_close).setVisibility(
//                        dragState != DragFlowLayout.DRAG_STATE_IDLE
//                                && data.isCanDrag() ? View.VISIBLE : View.INVISIBLE);
            }

            //根据指定的child获取对应的数据。
            @NonNull
            @Override
            public DragItem getData(View itemView) {
                return (DragItem) itemView.getTag();
            }
        });

        initData();
    }

    private void initObserver() {
//        //添加view观察者
        myColumn.addViewObserver(new IViewObserver() {
            @Override
            public void onAddView(View child, int index) {
                if (isEdit && null != moveItem&&!isAddOrRemove) {
                    myDrag.add(index, moveItem);
                    moveItem = null;
                }
                isAddOrRemove = false;
            }

            @Override
            public void onRemoveView(View child, int index) {
                if (isEdit&&!isAddOrRemove) {
                    moveItem = myDrag.get(index);
                    myDrag.remove(index);
                }
                isAddOrRemove = false;
            }
        });
    }

    private void initData() {
        if (ListUtils.isListValued(columnList)) {
            DragItem item;
            for (int i = 0; i < columnList.size(); i++) {
                FocusColumn focusColumn = columnList.get(i);
                if (focusColumn.isChoice()) {
                    item = new DragItem(focusColumn.getChannelId(), focusColumn.getChannelName(), focusColumn.getChannelType(), false);
                    myDrag.add(item);
                } else {
                    item = new DragItem(focusColumn.getChannelId(), focusColumn.getChannelName(), focusColumn.getChannelType(), false);
                    otherDrag.add(item);
                }
            }

            myColumn.getDragItemManager().addItems(myDrag);
            otherColumn.getDragItemManager().addItems(otherDrag);
            if (ListUtils.isListValued(otherDrag)) {
                otherColumn.setVisibility(View.VISIBLE);
            }
        }

    }

    private boolean isAddOrRemove = false;

    private void myDragRemove(DragItem data) {
        isAddOrRemove = true;
        myColumn.getDragItemManager().removeItem(data);
        DragItem item = (DragItem) data;
        item.setCanDrag(false);
        myDrag.remove(item);
        otherDrag.add(item);
        otherColumn.getDragItemManager().addItem(item);

    }

    private void OtherAdd(DragItem data) {
        isAddOrRemove = true;
        otherColumn.getDragItemManager().removeItem(data);
        DragItem item = (DragItem) data;
        item.setCanDrag(true);
        myDrag.add(item);
        otherDrag.remove(item);
        myColumn.getDragItemManager().addItem(item);

    }

    @OnClick({R.id.back_layout, R.id.to_edit})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_layout:
                onBackPressed();
                break;
            case R.id.to_edit:
                if (!isEdit) {
                    isEdit = true;
                    toEdit.setText("完成");
                    for (int i = 2; i < myDrag.size(); i++) {
                        myDrag.get(i).setCanDrag(true);
                    }
                    myColumn.getDragItemManager().clearItems();
                    myColumn.getDragItemManager().addItems(myDrag);

                    otherColumn.getDragItemManager().clearItems();
                    otherColumn.getDragItemManager().addItems(otherDrag);
                    initObserver();
                } else {
                    for (int j = 0; j < myDrag.size(); j++) {
                        for (int i = 0; i < columnList.size(); i++) {
                            FocusColumn focusColumn = columnList.get(i);
                            if (focusColumn.getChannelId() == myDrag.get(j).getId()) {
                                focusColumn.setChoice(true);
                                newColumnList.add(focusColumn);
                                columnList.remove(focusColumn);
                                break;
                            }
                        }
                    }
                    for (int j = 0; j < otherDrag.size(); j++) {
                        for (int i = 0; i < columnList.size(); i++) {
                            FocusColumn focusColumn = columnList.get(i);
                            if (focusColumn.getChannelId() == otherDrag.get(j).getId()) {
                                focusColumn.setChoice(false);
                                newColumnList.add(focusColumn);
                                columnList.remove(focusColumn);
                                break;
                            }
                        }
                    }
                    String s = initGson().toJson(newColumnList);
                    SharedPreferencesUtil.setParam(this, COLUMN_LIST, s);
                    Intent intent = getIntent();
                    intent.putExtra(SELECT_POS, 0);
                    intent.putExtra(COLUMN, (Serializable) newColumnList);
                    setResult(RESULT_CODE, intent);
                    finish();
                }

                break;
        }
    }

    private Gson initGson() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }
}
